# -*- coding: UTF-8 -*-
from django.shortcuts import render

# Create your views here.
from django.shortcuts import render, get_object_or_404, redirect
#引入验证登录的装饰器
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

from article.models import ArticlePost
from .forms import CommentForm

@login_required(login_url='/userprofile/login/')
def post_comment(request, article_id):
    article = get_object_or_404(ArticlePost, id=article_id)
    if request.method == 'POST':
        commentform  = CommentForm(data=request.POST)
        if commentform.is_valid():
            new_comment = commentform.save(commit=False)
            new_comment.article = article
            new_comment.user = request.user
            new_comment.save()
            return redirect(article)
        else:
            return HttpResponse("表单内容有误，请重新填写。")

    else:
        return HttpResponse("发表评论仅接受POST请求。")